/**
 * @file FileLogAppender.cpp
 * @author  haifeng (haifengxiao@icloud.com)
 * @brief
 * @version 0.1
 * @date 2021-07-06
 *
 * @copyright Copyright (c) 2021
 *
 */

#include "FileLogAppender.h"

namespace Chen {
    namespace Log {

        FileLogAppender::FileLogAppender(const std::string& filename)
            : m_filename(filename) {
            reopen();
        }

        void FileLogAppender::log(std::shared_ptr<Logger> logger, LogLevel::Level level, LogEvent::ptr event) {
            if (level >= m_level) {
                uint64_t now = event->getTime();
                if (now >= (m_lastTime + 3)) {
                    reopen();
                    m_lastTime = now;
                }
                //if(!(m_filestream << m_formatter->format(logger, level, event))) {
                if (!m_formatter->format(m_filestream, logger, level, event)) {
                    std::cout << "error" << std::endl;
                }
            }
        }

        bool FileLogAppender::reopen() {
            if (m_filestream) {
                m_filestream.close();
            }
            return true;
        }

    } //namespace Log
} //namespace Chen